package com.gxa.controller.set;

import com.gxa.common.R;
import com.gxa.entity.DistrictSysUser;
import com.gxa.service.DistrictUserService;
import com.gxa.utils.ImageHandle;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/districtUser")
@Api(value = "区级账号管理的所有操作测试",tags = "账号管理接口API")
public class DistrictUserController {

    @Autowired
    private DistrictUserService districtUserService;

    /**
     * 查询对应的区级账号并进行分页操作
     * @param currentPage
     * @param pageSize
     * @param area
     * @return
     */
    @RequestMapping(value = "/queryAllDistrictUsersByPage",method = RequestMethod.GET)
    @ApiOperation(value = "查询对应区级的所有账号接口",notes = "查询对应区级的所有账号进行分页显示",httpMethod = "GET",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码数",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示记录数",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "area",value = "所属区域",required = true,dataType = "string",paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R queryAllDistrictUsersByPage(int currentPage,int pageSize,String area){
        return districtUserService.queryAllDistrictUsersByPage(currentPage,pageSize,area);
    }

    /**
     * 按照条件查询对应的区级账号并进行分页操作
     * @param userName
     * @param phone
     * @param organization
     * @param nickname
     * @param role
     * @param status
     * @param currentPage
     * @param pageSize
     * @param area
     * @return
     */
    @RequestMapping(value = "/queryConditionalDistrictUser",method = RequestMethod.GET)
    @ApiOperation(value = "按条件查询账号接口",notes = "按条件查询账号信息进行分页显示",httpMethod = "GET",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "姓名",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "phone",value = "联系方式",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "organization",value = "所属组织",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "nickname",value = "账号",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "role",value = "所属角色",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "status",value = "状态",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "currentPage",value = "当前页码数",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示记录数",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "area",value = "所属区域",required = true,dataType = "string",paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R queryConditionalDistrictUser(String userName,String phone,String organization,String nickname,String role,String status,int currentPage,int pageSize,String area){
        return districtUserService.queryConditionalDistrictUser(userName,phone,organization,nickname,role,status,currentPage,pageSize,area);
    }

    /**
     * 添加账号信息
     * @param districtSysUser
     * @return
     */
    @RequestMapping(value = "/addDistrictUser",method = RequestMethod.POST)
    @ApiOperation(value = "添加账号接口",notes = "添加账号信息",httpMethod = "POST",response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R addDistrictUser(@RequestBody @ApiParam(name = "districtSysUser",value = "账号信息",required = true) DistrictSysUser districtSysUser){
        return districtUserService.addDistrictUser(districtSysUser);
    }

    /**
     * 修改账号
     * @param districtSysUser
     * @return
     */
    @RequestMapping(value = "/updateDistrictUser",method = RequestMethod.POST)
    @ApiOperation(value = "修改账号接口",notes = "修改账号信息",httpMethod = "POST",response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R updateDistrictUser(@RequestBody @ApiParam(name = "districtSysUser",value = "账号信息",required = true) DistrictSysUser districtSysUser){
        return districtUserService.updateDistrictUser(districtSysUser);
    }

    /**
     * 根据账号编号删除账号信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteDistrictUser",method = RequestMethod.POST)
    @ApiOperation(value = "删除账号接口",notes = "根据账号编号删除账号信息",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "id",value = "要删除的账号编号",required = true,dataType = "int",paramType = "query")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R deleteDistrictUser(int id){
        return districtUserService.deleteDistrictUserById(id);
    }

    /**
     * 启用账号
     * @param id
     * @return
     */
    @RequestMapping(value = "/enableDistrictUser",method = RequestMethod.POST)
    @ApiOperation(value = "启用账号接口",notes = "启用账号",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "id",value = "要启用的账号编号",required = true,dataType = "int",paramType = "query")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R enableDistrictUser(int id){
        return districtUserService.enableDistrictUser(id);
    }

    /**
     * 禁用账号
     * @return
     */
    @RequestMapping(value = "/disableDistrictUser",method = RequestMethod.POST)
    @ApiOperation(value = "禁用账号接口",notes = "禁用账号",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "id",value = "要禁用的账号编号",required = true,dataType = "int",paramType = "query")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R disableDistrictUser(int id){
        return districtUserService.disableDistrictUser(id);
    }

    /**
     * 重置账号密码为123456
     * @param id
     * @return
     */
    @RequestMapping(value = "/resetPassword",method = RequestMethod.POST)
    @ApiOperation(value = "重置密码",notes = "重置为默认密码123456",httpMethod = "POST",response = R.class)
    @ApiImplicitParam(name = "id",value = "要重置密码的账号编号",required = true,dataType = "int",paramType = "query")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R resetPassword(int id){
        return districtUserService.resetPassword(id);
    }

    /**
     * 根据账号ID查询账号信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryDistrictUserById",method = RequestMethod.GET)
    @ApiOperation(value = "根据账号ID查询账号信息接口",notes = "根据账号ID查询账号信息",httpMethod = "GET",response = R.class)
    @ApiImplicitParam(name = "id",value = "账号ID",required = true,dataType = "int",paramType = "query")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R queryDistrictUserById(int id){
        return districtUserService.queryDistrictUserById(id);
    }

    /**
     * 根据账号名称查询账号信息
     * @param userName
     * @return
     */
    @RequestMapping(value = "/queryDistrictUserByUserName",method = RequestMethod.GET)
    @ApiOperation(value = "根据账号名称查询账号信息接口",notes = "根据账号名称查询账号信息",httpMethod = "GET",response = R.class)
    @ApiImplicitParam(name = "userName",value = "账号名称",required = true,dataType = "string",paramType = "query")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R queryDistrictUserByUserName(String userName){
        return districtUserService.queryDistrictUserByUserName(userName);
    }

    /**
     * 上传头像
     * @param multipartFile
     * @param request
     * @return
     */
    @RequestMapping(value = "/changeAvatar",method = RequestMethod.POST)
    @ApiOperation(value = "上传账号头像接口",notes = "上传账号头像",httpMethod = "POST",response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message = "请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R changeAvatar(MultipartFile multipartFile,HttpServletRequest request){
        R r = new R();
        /*前端 UI 的 form 表单里面有用到图片上传的时候，需要在这里对 request 作以下处理*/
        // MultipartHttpServletRequest mulRequest = (MultipartHttpServletRequest) request;
        // String title = mulRequest.getParameter("title");//获取标题

        //判空
        if( multipartFile != null ){
            //将图片上传成功后的返回链接返回
            try {
                String imgPath = ImageHandle.uploadImageToCos(multipartFile);
                r.setCode(200);
                r.setMsg("图片上传成功！");
                r.setData(imgPath);
            } catch (IOException e) {
                e.printStackTrace();
                r.setCode(500);
                r.setMsg("图片上传失败！");
            }
        }
        return r;
//        try {
//            //获取文件名
//            String fileName = multipartFile.getOriginalFilename();
//            //获取文件保存路径
//            String fileSavePath = "/opt/upload";
//            File file = new File(fileSavePath);
//            if(!file.exists()){
//                file.mkdir();
//            }
//            UUID uuid = UUID.randomUUID();
//            String path = fileSavePath+"/"+uuid.toString()+fileName;
//            multipartFile.transferTo(new File(path));//真正进行文件上传操作
//            r.setCode(200);
//            r.setMsg("图片上传成功！");
//            r.setData(path);
//        } catch (IOException e) {
//            e.printStackTrace();
//            r.setCode(500);
//            r.setMsg("图片上传失败！");
//        }
    }

    /**
     * 修改密码
     * @param id
     * @param newPassword
     * @return
     */
    @RequestMapping(value = "/changePassword",method = RequestMethod.POST)
    @ApiOperation(value = "修改账号密码接口",notes = "修改账号密码",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "要修改密码的账号编号",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "newPassword",value = "账号新密码",required = true,dataType = "String",paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R changePassword(int id,String newPassword){
        return districtUserService.changePassword(id,newPassword);
    }
}
